package cn.dcw.pethome.pet.controller;


import cn.dcw.pethome.basic.exception.MyException;
import cn.dcw.pethome.basic.util.AjaxResult;
import cn.dcw.pethome.basic.util.PageList;
import cn.dcw.pethome.pet.domain.Pet;
import cn.dcw.pethome.pet.dto.PetDto;
import cn.dcw.pethome.pet.query.PetQuery;
import cn.dcw.pethome.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService service;

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Pet> getPet(@RequestBody PetQuery query){

        return service.getPet(query);
    }

    /**
    通过id查询单条宠物数据
     */
    @GetMapping("/{id}")
    public PetDto getPetById(@PathVariable("id") Long id){

        return service.getPetById(id);
    }
    /**
    宠物领养
     */
    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long id, HttpServletRequest request){
        try {
            service.adopt(id,request);
            return new AjaxResult(true,"操作成功");
        }catch (MyException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统错误");
        }

    }
    /**
     * 宠物上架
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            service.onsale(ids);
            return new AjaxResult(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常");
        }

    }
    /**
     * 宠物下架
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            service.offsale(ids);
            return new AjaxResult(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常");
        }
    }
    /**
     * 添加或编辑
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        try {
            service.addOrUpdate(pet);
            return new AjaxResult(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常");
        }
    }
    /**
     * 单个删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return new AjaxResult(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常");
        }
    }
    /**
     * 批量删除
     */
    @PostMapping("/dels")
    public AjaxResult dels(@RequestBody List<Long> ids){
        try {
            service.dels(ids);
            return new AjaxResult(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常");
        }
    }

}
